<?php

/**
 * @author ducnhat
 * @email ddnhat@gmail.com
 * @copyright 2014
 */

class Admin_Form_ValidateProfile{
	
    protected $_isError = false;
	protected $_messageError = null;
    protected $_arrData = null;
    protected $_classError = null;
    
    public function __construct($params = array(), $edit = false, $data = null){
        $translate = Zend_Registry::get('Zend_Translate');
        
        $validateFirstName = new Zend_Validate();
        $validateLastName = new Zend_Validate();
        $validateDateOfBirth = new Zend_Validate();
        $validateIdentityNumber = new Zend_Validate();
        $validateAddress = new Zend_Validate();
        $validatePhoneNumber = new Zend_Validate();
        
        $option = array('table' => 'profile', 'field' => 'title');
        
        if($edit){
            $option['exclude'] = array ('field' => 'id', 'value' => $params['id']);
        }
        
        $validateFirstName->addValidator(new Zend_Validate_NotEmpty, true)
                        ->addValidator(new Zend_Validate_StringLength(array('max' => 20)), true);
        
        $validateLastName->addValidator(new Zend_Validate_NotEmpty, true)
                        ->addValidator(new Zend_Validate_StringLength(array('max' => 20)), true);
        
        $validateDateOfBirth->addValidator(new Zend_Validate_Date("dd-MM-yyyy"), true);
                        
        $validateIdentityNumber->addValidator(new Zend_Validate_Digits(), true)
                                ->addValidator(new Zend_Validate_StringLength(array('max' => 12)), true);
        
        $validatePhoneNumber->addValidator(new Zend_Validate_Digits(), true)
                            ->addValidator(new Zend_Validate_StringLength(array('max' => 11)), true);
        
        $validateAddress->addValidator(new Zend_Validate_StringLength(array('max' => 150)), true);
        
        /*
        * Check First Name
        */
        
        if(!$validateFirstName->isValid($params['first_name'])){
            $this->_messageError['first_name'] = '<label class="control-label" for="inputError">' . $translate->_('first name') . current($validateFirstName->getMessages()) . '</label>';
            $this->_arrData['first_name'] = '';
            $this->_classError['first_name'] = 'has-error';
            $this->_isError = true;
            
            if($edit){
                $this->_arrData['first_name'] = $data['first_name'];
            }
        }else{
            $this->_messageError['first_name'] = '';
            $this->_arrData['first_name'] = $params['first_name'];
            $this->_classError['first_name'] = '';
        }
        
        /*
        * Check Last Name
        */
        
        if(!$validateLastName->isValid($params['last_name'])){
            $this->_messageError['last_name'] = '<label class="control-label" for="inputError">' . $translate->_('last name') . current($validateLastName->getMessages()) . '</label>';
            $this->_arrData['last_name'] = '';
            $this->_classError['last_name'] = 'has-error';
            $this->_isError = true;
            
            if($edit){
                $this->_arrData['last_name'] = $data['last_name'];
            }
        }else{
            $this->_messageError['last_name'] = '';
            $this->_arrData['last_name'] = $params['last_name'];
            $this->_classError['last_name'] = '';
        }
        
        /*
        * Check Date of Birth
        */
        
        $this->_messageError['date_of_birth'] = '';
        $this->_arrData['date_of_birth'] = $params['date_of_birth'];
        $this->_classError['date_of_birth'] = '';
        
        if(strlen($params['date_of_birth']) > 0){
            if(!$validateDateOfBirth->isValid($params['date_of_birth'])){
                $this->_messageError['date_of_birth'] = '<label class="control-label" for="inputError">' . $translate->_('date_of_birth') . current($validateDateOfBirth->getMessages()) . '</label>';
                $this->_arrData['date_of_birth'] = '';
                $this->_classError['date_of_birth'] = 'has-error';
                $this->_isError = true;
                
                if($edit){
                    $this->_arrData['date_of_birth'] = $data['date_of_birth'];
                }
            }
        }
        
        /*
        * Check Identity Number
        */
        
        $this->_messageError['identity_number'] = '';
        $this->_arrData['identity_number'] = $params['identity_number'];
        $this->_classError['identity_number'] = '';
        
        if(strlen($params['identity_number']) > 0){
            if(!$validateIdentityNumber->isValid($params['identity_number'])){
                $this->_messageError['identity_number'] = '<label class="control-label" for="inputError">' . $translate->_('identity number') . current($validateIdentityNumber->getMessages()) . '</label>';
                $this->_arrData['identity_number'] = '';
                $this->_classError['identity_number'] = 'has-error';
                $this->_isError = true;
                
                if($edit){
                    $this->_arrData['identity_number'] = $data['identity_number'];
                }
            }
        }
        
        /*
        * Check Address
        */
        
        $this->_messageError['address'] = '';
        $this->_arrData['address'] = $params['address'];
        $this->_classError['address'] = '';
        
        if(strlen($params['address']) > 0){
            if(!$validateAddress->isValid($params['address'])){
                $this->_messageError['address'] = '<label class="control-label" for="inputError">' . $translate->_('address') . current($validateAddress->getMessages()) . '</label>';
                $this->_arrData['address'] = '';
                $this->_classError['address'] = 'has-error';
                $this->_isError = true;
                
                if($edit){
                    $this->_arrData['address'] = $data['address'];
                }
            }
        }
        
        /*
        * Check Phone Number
        */
        
        $this->_messageError['phone_number'] = '';
        $this->_arrData['phone_number'] = $params['phone_number'];
        $this->_classError['phone_number'] = '';
        
        if(strlen($params['phone_number']) > 0){
            if(!$validatePhoneNumber->isValid($params['phone_number'])){
                $this->_messageError['phone_number'] = '<label class="control-label" for="inputError">' . $translate->_('phone number') . current($validatePhoneNumber->getMessages()) . '</label>';
                $this->_arrData['phone_number'] = '';
                $this->_classError['phone_number'] = 'has-error';
                $this->_isError = true;
                
                if($edit){
                    $this->_arrData['phone_number'] = $data['phone_number'];
                }
            }
        }
        
        if($params['gender'] == 1){
            $gender = true;
        }else{
            $gender = false;
        }
        
        $this->_arrData['gender'] = $gender;
        
        if($edit){
            $this->_arrData['id'] = $data['id'];
        }
        
        $date = new Zend_Date();
        $curDate = $date->get(Zend_Registry::get('date_format_insert'));
        
        $info = new Zenddn_System_Info();
        $user_id = $info->getUserInfo('id');
        
        if(!$edit){
            $this->_arrData['created'] = $curDate;
            $this->_arrData['created_by'] = $user_id;
        }
        
        $this->_arrData['modified'] = $curDate;
        $this->_arrData['modified_by'] = $user_id;
        $this->_arrData['avatar'] = $params['avatar'];
        
        if(!$this->isError()){
            $this->_arrData['date_of_birth'] = date("Y-m-d", strtotime($this->_arrData['date_of_birth']));
            $this->_arrData['user_id'] = $params['id'];
        }
    }
    
    public function isError(){
    	return $this->_isError;
    }
    
    public function getMessageError(){
        return $this->_messageError;
    }
    
    public function getClassError(){
    	return $this->_classError;
    }
    
    public function getData(){
        return $this->_arrData;
    }
}